/**
 * Octopus, an XMMS2 client.
 * Copyright (C) 2014 Octopus Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace OctopusClutterGtk {
	//js does not seem to handle structs (like Gdk.EventKey or Gdk.EventWindowState) well.
	public class ClutterGtkEventManager : Object {
		public Gtk.Window window { get; construct; }
		public bool fullscreen { get; private set; }
		public signal void key_event (uint keyval, string str, Gdk.ModifierType modifiers);

		public ClutterGtkEventManager (Gtk.Window window) {
			Object (window: window);
		}

		construct {
			window.window_state_event.connect (on_window_state_event);
			window.key_press_event.connect (on_key_press_event);
		}

		private bool on_window_state_event (Gdk.EventWindowState event) {
			GLib.debug ("event.new_window_state  %u", event.new_window_state);
			if ((event.new_window_state & Gdk.WindowState.FULLSCREEN) == Gdk.WindowState.FULLSCREEN) {
				GLib.debug ("WindowStateManager: fullscreen ON");
				fullscreen = true;
			} else {
				GLib.debug ("WindowStateManager: fullscreen OFF");
				fullscreen = false;
			}
			return false; //propagate
		}

		private bool on_key_press_event (Gdk.EventKey event) {
			key_event (event.keyval, event.str, event.state);
			return false; //propagate
		}
	}

	public class BlockReturnKeyPress : Object {
		public Gtk.Widget widget { get; construct; }

		public BlockReturnKeyPress (Gtk.Widget widget) {
			Object (widget: widget);
		}

		construct {
			widget.key_press_event.connect (on_key_press_event);
		}

		private bool on_key_press_event (Gdk.EventKey event) {
			if (   ((event.state & Gdk.ModifierType.SHIFT_MASK) != Gdk.ModifierType.SHIFT_MASK)
			    && (event.keyval == Gdk.Key.Return)) {
				return true; //stop
			}
			return false; //propagate
		}
	}

	public static void file_dialog () {
		GLib.debug ("fileDialog");
		var file_chooser = new Gtk.FileChooserDialog ("Open File", null,
		                                              Gtk.FileChooserAction.OPEN,
		                                              Gtk.Stock.CANCEL, Gtk.ResponseType.CANCEL,
		                                              Gtk.Stock.OPEN, Gtk.ResponseType.ACCEPT);
        if (file_chooser.run () == Gtk.ResponseType.ACCEPT) {
			GLib.debug ("fileDialog: %s", file_chooser.get_filename ());
        }
        file_chooser.destroy ();
	}
}
